﻿using System.Buffers;
using Letter.IO.Buf;

namespace BufTest;

class Program
{
    private static string[] arr =
    {
        "jianjie_0",
        "jianjie_1",
        "jianjie_2",
        "jianjie_3",
        "jianjie_4",
        "jianjie_5",
        "jianjie_6",
        "jianjie_7",
        "jianjie_8",
        "jianjie_9",
    };
    
    static async Task Main(string[] args)
    {
        var buffer = BufferProvider.Create<string>();

        //var buffer = new Buffer1<string>(BufferOptions<string>.Default);

        while (true)
        {
            string str = Console.ReadLine();
            if (str == "w")
            {
                try
                {
                    var length = arr.Length;
                    Console.WriteLine("每段长度::" + length);
                    for (int i = 0; i < 100_000; i++)
                    {
                        //buffer.Write
                        var memory = buffer.GetWritableSequence(length);
                        new ReadOnlyMemory<string>(arr).CopyTo(memory);
                        buffer.WriterAdvance(length);

                        buffer.Flush();

                    }

                    Console.WriteLine("当前buffer长度：：" + buffer.ReadableLength);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    
                }
                
            }

            if (str == "r")
            {
                try
                {
                    Console.WriteLine("0000000---当前buffer长度：：" + buffer.ReadableLength);
                    var result = buffer.GetReadableSequence();
                    Console.WriteLine("1111111---读到的budder长度：" + result.Length);

                    Console.WriteLine("reader 向前");
                    buffer.ReaderAdvanceTo(result.End);

                    var result1 = buffer.GetReadableSequence();
                    Console.WriteLine("2222222---读到的budder长度：" + result1.Length);
                    Console.WriteLine("3333333---当前buffer长度：：" + buffer.ReadableLength);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    
                }
            }


            if (str == "c")
            {
                try
                {
                    buffer.Clear();
                    Console.WriteLine("3333333---当前buffer长度：：" + buffer.ReadableLength);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    
                }
            }


            if (str == "rl")
            {
                buffer.Release();
            }

            if (str == "re")
            {
                buffer.Reset();
            }
        }
    }


        // await Local.Start();
}


    

